import pygame, sys
from pygame.locals import *
import Sprite

class Scene:
    def __init__(self, screen):
        self.screen = screen
        self.player_group = pygame.sprite.Group()
        self.fire_group = pygame.sprite.Group()
        self.explosion_group = pygame.sprite.Group()

        # 创建玩家精灵
        self.player = Sprite.NSprite()
        self.player.load("./Resource/player_walk.png", 96, 96, 8, 8)
        self.player.position = 0, 300
        self.player.mspeed = 3
        self.player_group.add(self.player)

    def ProcKey(self, ticks):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                print("key down ", event.key, pygame.K_ESCAPE)
                if event.key == pygame.K_SPACE:
                    self.Fire()

        keys = pygame.key.get_pressed()
        if keys[K_ESCAPE]:
            pygame.quit()
            sys.exit()
        elif keys[K_UP] or keys[K_w]:
            self.player.SetMoveDir(0)
        elif keys[K_RIGHT] or keys[K_d]:
            self.player.SetMoveDir(2)
        elif keys[K_DOWN] or keys[K_s]:
            self.player.SetMoveDir(4)
        elif keys[K_LEFT] or keys[K_a]:
            self.player.SetMoveDir(6)
        else:
            self.player.Stop()

    def Update(self, ticks):
        self.screen.fill((50, 50, 100))
        self.player_group.update(ticks, 60)
        self.player_group.draw(self.screen)
        self.fire_group.update(ticks, 60)
        self.fire_group.draw(self.screen)
        self.explosion_group.update(ticks, 60)
        self.explosion_group.draw(self.screen)
        pygame.display.flip()

    def Fire(self):
        fire = Sprite.NSprite()
        filename = ""
        width = 0
        height = 0
        begin = self.player.rect.center
        if self.player.direction == 0:
            filename = "./Resource/bullet_up.png"
            width = 16
            height = 40
            begin = self.player.rect.midtop
        elif self.player.direction == 2:
            filename = "./Resource/bullet_right.png"
            width = 40
            height = 16
            begin = self.player.rect.midright
        elif self.player.direction == 4:
            filename = "./Resource/bullet_down.png"
            width = 16
            height = 40
            begin = self.player.rect.midbottom
        elif self.player.direction == 6:
            filename = "./Resource/bullet_left.png"
            width = 40
            height = 16
            begin = self.player.rect.midleft
        fire.load(filename, width, height, 1, 1)
        fire.mspeed = 6
        fire.rect.center = begin

        fire.SetMoveDir(self.player.direction)
        self.fire_group.add(fire)

        self.Explosion(begin)

    def Explosion(self, position):
        explosion = Sprite.NSprite()
        explosion.load("./Resource/explosion.png", 128, 128, 6, 5)
        explosion.position = position
        self.explosion_group.add(explosion)